package edu.zhku.ttms.view.panel.content;

import edu.zhku.ttms.domain.TrafficPoliceman;
import edu.zhku.ttms.service.TrafficPolicemanService;
import edu.zhku.ttms.service.impl.TrafficPolicemanServiceImpl;
import edu.zhku.ttms.utils.SystemConst;
import edu.zhku.ttms.view.ViewController;
import edu.zhku.ttms.view.button.*;
import edu.zhku.ttms.view.dialog.TrafficPolicemanAddDialog;
import edu.zhku.ttms.view.dialog.TrafficPolicemanModDialog;

import javax.swing.*;
import java.util.List;

/**
 * @author 泽
 * @since 2021/4/24 13:12
 */
public class TrafficPoliceManagementPanel extends AbstractManagementPanel {

    static TrafficPolicemanService trafficPolicemanService = new TrafficPolicemanServiceImpl();

    @Override
    public void loadData(String queryStr, int page) {

        this.setVisible(false);

        dataPanel.removeAll();

        dataPanel.setBounds(100, 70, 960, 540);

        String[] columnNames = {"序号", "交警代号", "姓名", "所属分局"};

        List<TrafficPoliceman> list = trafficPolicemanService.getList(queryStr);
        Object[][] data = new Object[list.size()][columnNames.length];
        for (int i = 0; i < list.size(); i++) {
            TrafficPoliceman policeman = list.get(i);
            String policeCode = policeman.getPoliceCode();
            String name = policeman.getName();
            String branch = policeman.getBranch();
            data[i] = new Object[]{i + 1, policeCode, name, branch};
        }

        int x = 0;
        int y = 0;

        int width = 150;
        int height = 30;
        int buttonWidth = 70;
        int buttonHeight = 25;

        int totalRow = data.length;

        int fromIndex = SystemConst.PAGE_SIZE * (page - 1);
        int toIndex = Math.min(SystemConst.PAGE_SIZE * page, totalRow);

        for (int i = 0; i < columnNames.length; i++) {
            JLabel jLabel = new JLabel(columnNames[i]);
            jLabel.setBounds(x + i * width, y, width, height);
            dataPanel.add(jLabel);
        }

        for (int i = 0; i < toIndex - fromIndex; i++) {
            for (int j = 0; j < data[0].length; j++) {
                JLabel jLabel = new JLabel(data[fromIndex + i][j].toString());
                jLabel.setBounds(x + j * width, y + height + i * height, width, height);
                dataPanel.add(jLabel);
            }
            TrafficPoliceman policeman = list.get(i + fromIndex);
            Long policeId = policeman.getPoliceId();

            JButton modifyButton = new ModifyButton();
            modifyButton.addActionListener(e -> new TrafficPolicemanModDialog(policeman).setVisible(true));
            modifyButton.setBounds(x + columnNames.length * width, y + height + i * height, buttonWidth, buttonHeight);
            dataPanel.add(modifyButton);

            JButton deleteButton = new DeleteButton();
            deleteButton.addActionListener(e -> {
                int deleteConfirm = JOptionPane.showConfirmDialog(this, "确定要删除该交警信息？", "确认信息", JOptionPane.YES_NO_OPTION);
                if (deleteConfirm == JOptionPane.YES_OPTION) {
                    trafficPolicemanService.removeById(policeId);
                    ViewController.changeView(new TrafficPoliceManagementPanel());
                }
            });
            deleteButton.setBounds(x + buttonWidth + 10 + columnNames.length * width, y + height + i * height, buttonWidth, buttonHeight);
            dataPanel.add(deleteButton);
        }

        JButton addPolicemanButton = new AddButton();
        addPolicemanButton.setBounds(780, 40, buttonWidth, buttonHeight);
        addPolicemanButton.addActionListener(e -> new TrafficPolicemanAddDialog().setVisible(true));
        this.add(addPolicemanButton);

        int pageY = y + (SystemConst.PAGE_SIZE + 1) * height;
        int totalPage = Math.max((int)Math.ceil((double)totalRow / SystemConst.PAGE_SIZE), 1);
        JLabel pageInfoLabel = new JLabel("第" + page + "页，共" + totalPage + "页");
        pageInfoLabel.setBounds(x, pageY, width, buttonHeight);
        dataPanel.add(pageInfoLabel);

        boolean hasPrePageButton = page > 1;
        boolean hasNextPageButton = toIndex < totalRow;
        if (hasPrePageButton) {
            JButton prePageButton = new PrePageButton();
            prePageButton.addActionListener(e -> loadData(queryStr, page - 1));
            prePageButton.setBounds(x + width, pageY, buttonWidth, buttonHeight);
            dataPanel.add(prePageButton);
        }
        if (hasNextPageButton) {
            JButton nextPageButton = new NextPageButton();
            nextPageButton.addActionListener(e -> loadData(queryStr, page + 1));
            nextPageButton.setBounds(hasPrePageButton ? x + 2 * width : x + width, pageY, buttonWidth, buttonHeight);
            dataPanel.add(nextPageButton);
        }

        this.add(dataPanel);
        this.setVisible(true);
    }
}
